Skip to main content

Install and create your DID


Pre requirements:

You must have installed:

  • NODEJS
  • NPM
  • TYPESCRIPT
  • TS-NODE

1. Create a directory

mkdir quarkid-app
cd quarkid-app

2. Install

Use NPM to install the following packages:

npm init
npm install @extrimian/did-registry @extrimian/did-core @extrimian/kms-client @extrimian/kms-core

3. Create the keys

To create a DID you need to provide your public keys. You can use the Extrimian KMS component to generate your public and private keys.

Key Management Service

KMS applies dependency inversion concepts, so it requires passing a SecureStorage in its constructor. This time, we will use FileSystem to store the data and access it in a simple way. Create a file storage.ts with the following code:

import { readFileSync, writeFileSync, existsSync } from "fs";
import { KMSStorage } from "@extrimian/kms-core";

export class FileSystemKMSSecureStorage implements KMSStorage {
public readonly filepath: string;

constructor(params: { filepath: string }) {
this.filepath = params.filepath;
}

async add(key: string, data: any): Promise<void> {
const map = this.getData();
map.set(key, data);
this.saveData(map);
}

async get(key: string): Promise<any> {
return this.getData().get(key);
}

async getAll(): Promise<Map<string, any>> {
return this.getData();
}

update(key: string, data: any) {
const map = this.getData();
map.set(key, data);
this.saveData(map);
}

remove(key: string) {
const map = this.getData();
map.delete(key);
this.saveData(map);
}

private getData(): Map<string, any> {
if (!existsSync(this.filepath)) {
return new Map();
}

const file = readFileSync(this.filepath, {
encoding: "utf-8",
});

if (!file) {
return new Map();
}

return new Map(Object.entries(JSON.parse(file)));
}

private saveData(data: Map<string, any>) {
writeFileSync(this.filepath, JSON.stringify(Object.fromEntries(data)), {
encoding: "utf-8",
});
}
}
  • KMS Keys

    Create a ts file did.ts.

Import the following dependencies.

import { FileSystemKMSSecureStorage } from "./storage";
import { KMSClient } from "@extrimian/kms-client";
import { LANG, Suite } from "@extrimian/kms-core";

Create a function called createDID as shown below.

Create the recoveryKey, updateKey, bbsBlsJwk and didCommJwk.

export const createDID = async () => {
const kms = new KMSClient({
lang: LANG.en,
storage: new FileSystemKMSSecureStorage({
filepath: "file-system-storage",
}),
});

const updateKey = await kms.create(Suite.ES256k);
const recoveryKey = await kms.create(Suite.ES256k);
const didComm = await kms.create(Suite.DIDComm);
const bbsbls = await kms.create(Suite.Bbsbls2020);
};

To obtained more information about Extrimian KMS, see the documentation at: @extrimian/kms-client

4. Create a long DID

With the DID creation service, you can create a LONG DID, which is a DID where its DID Document is embedded in the information returned in a base64 format. It is a self-resolvable DID, meaning that by decoding the base64, you can obtain its DID Document.

In the did.ts file import the dependencies and add the code as shown below.

import { Did } from "@extrimian/did-registry";
import {
AssertionMethodPurpose,
KeyAgreementPurpose,
} from "@extrimian/did-core";
const didService = new Did();

const createDidResponse = await didService.createDID({
updateKeys: [updateKey.publicKeyJWK],
recoveryKeys: [recoveryKey.publicKeyJWK],
verificationMethods: [
{
id: "bbsbls",
type: "Bls12381G1Key2020",
publicKeyJwk: bbsbls.publicKeyJWK,
purpose: [new AssertionMethodPurpose()],
},
{
id: "didComm",
type: "X25519KeyAgreementKey2019",
publicKeyJwk: didComm.publicKeyJWK,
purpose: [new KeyAgreementPurpose()],
},
],
});

return createDidResponse;

5. Try your DID

Write the following code to try if your did is created properly.

console.log(JSON.stringify(createDidResponse.longDid));

Execute the function.

createDID();

Open a terminal and use ts-node to execute your code.

ts-node did.ts